<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="商品名称:" prop="goodsName">
              <span>{{dataForm.goodsName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="商家名称:" prop="trueName">
              <span>{{dataForm.trueName}}</span>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="商家账号:" prop="userName">
              <span>{{dataForm.userName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="商家营业时间:" prop="msn">
              <span>{{dataForm.msn}}</span>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="商家地址:" prop="address">
              <span>{{dataForm.address}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="商品图片:" prop="goodsImg">
                <el-upload
                action=""
                list-type="picture-card"
                :file-list="dataForm.goodsImg"
                :on-preview="handlePreview">
                <i class="el-icon-plus"></i>
                </el-upload>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="领券量:" prop="saleCount">
              <span>{{dataForm.saleCount}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="上架/下架:" prop="isSale">
              <span v-if="dataForm.isSale === 1">上架</span>
              <span v-if="dataForm.isSale === 0">下架</span>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="券规则:" prop="goodsRule">
              <span>{{dataForm.goodsRule}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="券描述:" prop="goodsDesc">
              <textarea v-model="dataForm.goodsDesc" placeholder="券描述"></textarea>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="审核结果:" prop="address">
              <span v-if="dataForm.goodsStatus == -1">禁售</span>
              <span v-if="dataForm.goodsStatus == 0">待审核</span>
              <span v-if="dataForm.goodsStatus == 1">审核通过</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="创建时间:" prop="createTime">
              <span>{{dataForm.createTime}}</span>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="上架时间:" prop="saleTime">
              <span>{{dataForm.saleTime}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="下架时间:" prop="shelvesTime">
              <span>{{dataForm.shelvesTime}}</span>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="审核说明:" prop="statusRemarks">
              <span>{{dataForm.statusRemarks}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="券现有库存:" prop="goodsStock">
              <span>{{dataForm.goodsStock}}</span>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="券初始库存" prop="initialInventory">
              <span>{{dataForm.initialInventory}}</span>
            </el-form-item>
          </el-col>
      </el-row>
    </el-form>
    <!-- <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span> -->
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          goodsId: 0,
          userId: '',
          goodsName: '',
          trueName: '',
          userName: '',
          msn: '',
          address: '',
          goodsImg: [],
          saleCount: '',
          isSale: '',
          goodsRule: '',
          goodsDesc: '',
          goodsStatus: '',
          createTime: '',
          saleTime: '',
          shelvesTime: '',
          statusRemarks: '',
          goodsStock: '',
          initialInventory: ''
        },
        dataRule: {
        }
      }
    },
    methods: {
      // 图片预览
      handlePreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      init (goodsId) {
        this.dataForm.goodsId = goodsId || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.goodsId) {
            this.$http({
              url: this.$http.adornUrl(`/coupon/queryCouponDetailsById/${this.dataForm.goodsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.data.goodsId
                this.dataForm.userId = data.data.userId
                this.dataForm.goodsName = data.data.goodsName
                this.dataForm.trueName = data.data.trueName
                this.dataForm.userName = data.data.userName
                this.dataForm.msn = data.data.msn
                this.dataForm.address = data.data.address
                var pic = data.data.goodsImg
                this.dataForm.goodsImg.push({name: 'tupian', url: 'http://image.yilianbaihui.cn/consumer/goods/' + pic})
                this.dataForm.saleCount = data.data.saleCount
                this.dataForm.isSale = data.data.isSale
                this.dataForm.goodsRule = data.data.goodsRule
                this.dataForm.goodsStatus = data.data.goodsStatus
                this.dataForm.createTime = data.data.createTime
                this.dataForm.saleTime = data.data.saleTime
                this.dataForm.shelvesTime = data.data.shelvesTime
                this.dataForm.statusRemarks = data.data.statusRemarks
                this.dataForm.goodsStock = data.data.goodsStock
                this.dataForm.initialInventory = data.data.initialInventory
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/shoppinguser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'goodsId': this.dataForm.goodsId || undefined,
                'goodsName': this.dataForm.goodsName,
                'deletestatus': this.dataForm.trueName,
                'msn': this.dataForm.userName,
                'qq': this.dataForm.msn,
                'ww': this.dataForm.address,
                'address': this.dataForm.goodsImg,
                'availablebalance': this.dataForm.saleCount,
                'birthday': this.dataForm.isSale,
                'email': this.dataForm.goodsRule,
                'freezeblance': this.dataForm.goodsDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
